﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ingesup_ERP.Models;

namespace ingesup_ERP.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class EleveController : Controller
    {
        private db db = new db();

        //
        // GET: /Admin/Eleve/

        public ViewResult Index()
        {
            var eleve = db.ELEVE.Include("CHARGEDEPLACEMENT").Include("CLASSE");
            return View(eleve.ToList());
        }

        //
        // GET: /Admin/Eleve/Details/5

        public ViewResult Details(string id)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            return View(eleve);
        }

        //
        // GET: /Admin/Eleve/Create

        public ActionResult Create()
        {
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS");
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE");
            return View();
        } 

        //
        // POST: /Admin/Eleve/Create

        [HttpPost]
        public ActionResult Create(ELEVE eleve)
        {
            if (ModelState.IsValid)
            {
                foreach (string r in Roles.GetAllRoles())
                {
                    Console.WriteLine(r);
                }
                eleve.LOGINPERS = eleve.PRENOMPERS[0].ToString() + eleve.NOMPERS;
                eleve.MDPPERS = Functions.getHash(eleve.MDPPERS);
                db.ELEVE.AddObject(eleve);
                db.SaveChanges();
                Roles.AddUserToRole(eleve.LOGINPERS, db.PersonneView.Single(m => m.LOGIN == eleve.LOGINPERS).TYPE);
                return RedirectToAction("Index");  
            }

            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.IDCLASSE);
            return View(eleve);
        }
        
        //
        // GET: /Admin/Eleve/Edit/5
 
        public ActionResult Edit(string id)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.IDCLASSE);
            return View(eleve);
        }

        //
        // POST: /Admin/Eleve/Edit/5

        [HttpPost]
        public ActionResult Edit(ELEVE eleve)
        {
            if (ModelState.IsValid)
            {
                db.ELEVE.Attach(eleve);
                db.ObjectStateManager.ChangeObjectState(eleve, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.IDCLASSE);
            return View(eleve);
        }

        //
        // GET: /Admin/Eleve/Delete/5
 
        public ActionResult Delete(string id)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            return View(eleve);
        }

        //
        // POST: /Admin/Eleve/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            db.ELEVE.DeleteObject(eleve);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}